<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Money extends CI_Controller {

    function __construct(){
        parent::__construct();
        $this->load->model('MoneyModel','mm'); //加载model模型类
        $this->load->helper('cookie');
    }
    public function home(){
        $a=$this->db->where('phone',$_COOKIE['phone'])->select('code')->from('relationship')->join('user', 'user.id = relationship.user_id')->get()->result();

        $this->load->View('money/money.html',['list'=>$a]); //传递数据到view中
    }


    function code($len, $chars=null)//随机生成六位数的字母数字混合密码
    {
        if (is_null($chars)) {
            $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        }
        mt_srand(10000000*(double)microtime());
        for ($i = 0, $str = '', $lc = strlen($chars)-1; $i < $len; $i++) {
            $str .= $chars[mt_rand(0, $lc)];
        }
        return $str;
    }



    function index(){

                   $data=$this->input->post();//接收传过来的值
                   $cookie=$_COOKIE['phone'];//提取cookie里面的phone
                   $time=time();//添加时间戳
                   $vip= $this->db->where('phone',$cookie)->select('money')->from('relationship')->join('user', 'user.id = relationship.user_id')->get()->row_array();
                       if(!$vip) {
                           $code=$this->code(6);
                        $fid=array(
                            'fid'=>0,
                            'user_id'=> $_COOKIE['id'],
                            'code'=> $code,
                            'create_time'=>time()
                        );
                        $this->db->insert('relationship',$fid);//向数据库添加用户信息
                           echo "<script type='text/javascript'> alert('您已成为分销商'); location.href = '/index.php/money/home';</script>";

                       }else{



                   $a= $this->db->where('phone',$cookie)->select('fid')->from('relationship')->join('user', 'user.id = relationship.user_id')->get()->row_array();
                   if($a['fid']==0){return ;}  //寻找有没有上级如果没有就return出去，如果有的话就继续向下执行

          //这是第一级分销
                //取出自己的信息
                $one=$this->db->where('relationship.id',$a['fid'])->select('money,relationship.id,phone,fid')->from('relationship')->join('user', 'user.id = relationship.user_id')->get()->row_array();
                //计算上一级充值的利率
                $f=$data['money'] * 0.1;
                //余额和利率相加
                $u=$f+$one['money'];
                $date=array(
                    'money'=>$u
                );
                //将金额修改相加的金额
                $this->db->update('relationship',$date,array('id'=>$one['id']));
                // echo $this->db->last_query();die();//打印sql语句
                //添加金额来源
                $co=array(
                    'relationship_id'=>$one['id'],
                    'contect'=>'来自下线'.$cookie.'利率'.$f.'元',
                    'create_time'=>$time,
                );
                $this->db->insert('contect',$co);
                // echo $this->db->last_query();die();
                  if($one['fid']==0){return ;}  //寻找有没有上级如果没有就return出去，如果有的话就继续向下执行

         //这是第二级分销
                //取出自己现有的余额
                $two=$this->db->where('relationship.id',$one['fid'])->select('money,relationship.id,phone,fid')->from('relationship')->join('user', 'user.id = relationship.user_id')->get()->row_array();
                //计算上一级充值的利率
                $ff=$data['money'] * 0.06;
                //余额和利率相加
                $uu=$ff+$two['money'];
                $date=array(
                    'money'=>$uu
                );
                //将金额修改相加的金额
                $this->db->update('relationship',$date,array('id'=>$two['id']));
                // echo $this->db->last_query();die();//打印sql语句
                //添加金额来源
                $con=array(
                    'relationship_id'=>$two['id'],
                    'contect'=>'来自下线'.$one['phone'].'的下线'.$cookie.'利率'.$ff.'元',
                    'create_time'=>$time,
                );
                $this->db->insert('contect',$con);
                if($two['fid']==0){return ;} //寻找有没有上级如果没有就return出去，如果有的话就继续向下执行

        //这是第三级分销
                //取出自己现有的余额
                $three=$this->db->where('relationship.id',$two['fid'])->select('money,relationship.id,phone,fid')->from('relationship')->join('user', 'user.id = relationship.user_id')->get()->row_array();
                //计算上一级充值的利率
                $fff=$data['money'] * 0.03;
                //余额和利率相加
                $uuu=$fff+$three['money'];
                $date=array(
                    'money'=>$uuu
                );
                //将金额修改相加的金额
                $this->db->update('relationship',$date,array('id'=>$three['id']));
                // echo $this->db->last_query();die();//打印sql语句
                //添加金额来源
                $co=array(
                    'relationship_id'=>$three['id'],
                    'contect'=>'来自下线'.$two['phone'].'的下线'.$one['phone'].'的下线'.$cookie.'利率'.$fff.'元',
                    'create_time'=>$time,
                );
                $this->db->insert('contect',$co);
                if($three['fid']==0){return ;}  //寻找有没有上级如果没有就return出去，如果有的话就继续向下执行
                           echo "<script type='text/javascript'> alert('充值成功'); location.href = '/index.php/money/home';</script>";

          }

    }


}